package com.anjue.bitmap.display
{
	import com.anjue.bitmap.bmpinterface.IBmpMovieClip;
	import com.anjue.utils.BitmapDataInfo;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 位图影片剪辑
	 * @author anjue
	 */
	public class BmpMovieClip extends Sprite implements IBmpMovieClip
	{
		private var bmpFrames:Vector.<BitmapDataInfo>;
		private var _curFrame:uint = 1;
		private var _totFrame:uint = 0;
		private var _loop:Boolean = true;
		private var bmp:Bitmap;
		
		public function BmpMovieClip()
		{
			init();
		}
		
		private function init():void
		{
			bmp = new Bitmap();
			this.addChild(bmp);
		}
		
		public function set frames(v:Vector.<BitmapDataInfo>):void
		{
			bmpFrames = v;
			_totFrame = bmpFrames.length;
			addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		
		private function enterFrame(e:Event):void
		{
			if(_curFrame == _totFrame)
			{
				if(loop)
				{
					_curFrame = 1;
				}else
				{
					stop();
					return;
				}
			}
				
//			if(bmpFrames[_curFrame-1] && bmpFrames[_curFrame-1] is BitmapDataInfo)
//			{
//				var info:BitmapDataInfo = bmpFrames[_curFrame-1] as BitmapDataInfo;
				bmp.bitmapData = bmpFrames[_curFrame-1].bitmapData;
				bmp.x = bmpFrames[_curFrame-1].offsetx;
				bmp.y = bmpFrames[_curFrame-1].offsety;
//			}else
//			{
//				throw Error("第" + _curFrame + "帧的位图数据不存在!")
//			}
			_curFrame++;
		}
		
		public function play():void
		{
			if(!this.hasEventListener(Event.ENTER_FRAME))
				this.addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		
		public function stop():void
		{
			if(this.hasEventListener(Event.ENTER_FRAME))
				this.removeEventListener(Event.ENTER_FRAME,enterFrame);
		}
		
		public function gotoAndPlay(v:uint):Boolean
		{
			var b:Boolean;
			if(v>0 && v<_totFrame)
			{
				_curFrame = v;
				b = true;
			}else
			{
				b = false;
			}
			if(!this.hasEventListener(Event.ENTER_FRAME))
				this.addEventListener(Event.ENTER_FRAME,enterFrame);
			
			return b;
		}
		
		public function gotoAndStop(v:uint):Boolean
		{
			var b:Boolean;
			if(v>0 && v<_totFrame)
			{
				_curFrame = v;
				
				if(bmpFrames[_curFrame-1] && bmpFrames[_curFrame-1] is BitmapDataInfo)
				{
					var info:BitmapDataInfo = bmpFrames[_curFrame-1] as BitmapDataInfo;
					bmp.bitmapData = info.bitmapData;
					bmp.x = info.offsetx;
					bmp.y = info.offsety;
				}else
				{
					throw Error("第" + _curFrame + "帧的位图数据不存在!")
				}
				b = true;
			}else
			{
				b = false;
			}
			if(this.hasEventListener(Event.ENTER_FRAME))
				this.removeEventListener(Event.ENTER_FRAME,enterFrame);
				
			return b;
		}
		
		public function set loop(v:Boolean):void
		{
			_loop = v;
		}
		
		public function get loop():Boolean
		{
			return _loop;
		}
		
		public function get curFrame():uint
		{
			return _curFrame;
		}
		
		public function get totFrame():uint
		{
			return _totFrame;
		}
	}
}